package org.example.singletonPattern;

/**
 * 懒汉式单例（线程安全版）
 * 使用synchronized保证线程安全
 * 优点：线程安全，延迟初始化
 * 缺点：每次获取实例都要同步，性能开销大
 */
public class ThreadSafeSingleton {
    private static ThreadSafeSingleton instance;

    private ThreadSafeSingleton() {
        System.out.println("线程安全懒汉式单例实例被创建");
    }

    // 使用synchronized关键字保证线程安全
    public static synchronized ThreadSafeSingleton getInstance() {
        if (instance == null) {
            instance = new ThreadSafeSingleton();
        }
        return instance;
    }

    public void showMessage() {
        System.out.println("这是线程安全的懒汉式单例的方法调用");
    }
}